<?php
    class Database {
        private static $_instance;
        private $_connection;
        
        private function __construct() {
            $this->_connection = mysql_connect('localhost', DB_USER, DB_PASS);
        }
        
        private function getResultSet($query) {
            mysql_select_db(DB_DATABASE, $this->_connection);
            $resource =  mysql_query($query);
            $rsltArray = array();
            
            while($row = mysql_fetch_assoc($resource)) {
                $rsltArray[] = $row;
            }
            
            return new ResultSet($rsltArray);
        }
        
        public function update($tables, $columnValuePair, $where = null) {
            $query = "UPDATE $tables SET ";
            
            foreach($columnValuePair as $key => $value) {
                $value = mysql_real_escape_string($value);
                $query .= "$key = '$value', ";
            }
            $query = substr($query, 0, strlen($query) - 1);
            
            if (!($where === null)) {
                $query .= " WHERE ";
                foreach($where as $key => $value) {
                    $value = mysql_real_escape_string($value);
                    $query = "$key='$value', ";
                }
                $query = substr($query, 0, strlen($query) - 1) . ';';
            }
            
            return $this->getResultSet($query);
        }
        
        public function select($columns, $where = null, $table = DB_TABLE) {
            $query = 'SELECT';
            if (is_array($columns)) {
                foreach ($columns as $key => $value) {
                    $query .= " $value,";
                }
                $query = substr($query, 0, strlen($query) - 1) . " FROM $table";
            } else {
                $query .= " $columns FROM $table";
            }
            
            if (isset($where)) {
                $query .= " WHERE";
                foreach($where as $key => $value) {
                    $query .= " $key = '$value',";
                }
            }
            
            $query = substr($query, 0, strlen($query) - 1) . ';';
            
            return $this->getResultSet($query);
        }
        
        public function insert($columnValuePair, $table = DB_TABLE) {
            $query = sprintf("INSERT INTO %s (%s) VALUES ('%s')", 
                             $table,
                             array_map(mysql_real_escape_string, array_keys($columnValuePair)),
                             array_map(mysql_real_escape_string, $columnValuePair));
            
            return $this->getResultSet($query);        
        }
        
        public function runRawQuery($queryString) {
            return $this->getResultSet($queryString);
        }
        
        public static function getInstance() {
            if (!isset(self::$_instance)) {
                self::$_instance = new Database();
            }
            
            return self::$_instance;
        }
    }
?>